var gotoCarAnaFaultInfo;
require(['highcharts', 'reportCommon'], function () {
    var carId, carName, titleStr = "", dateType = "02";

    function carChange(event, treeId, treeNode) {
        if (treeNode.type === 'CAR') {
            carId = treeNode.id;
            carName = treeNode.name;
            //标题时间
            titleStr = chartTitle(dateType);
            //故障发生数  线路平均数  排名
            getCarAnaIdx(carId, dateType);
            //每日故障数据
            getCarAnaTrend(carId, dateType);
            //故障排名  饼图  柱状图
            getCarAnaRank(carId, dateType);
        } else {
            $.messager.alert("提示", "请选择一辆车进行查看。");
        }
    }

    //左侧树
    commonFun.leftTreeNodeClickObj['carAnaFaultInfo'] = carChange;

    $(".time-select a").click(function () {
        if (carId) {
            dateType = $(this).attr('val');
            //标题时间
            titleStr = chartTitle(dateType);
            $(this).addClass("active");
            $(this).siblings().removeClass("active");
            //故障发生数  线路平均数  排名
            getCarAnaIdx(carId, dateType);
            //每日故障数据
            getCarAnaTrend(carId, dateType);
            //故障排名  饼图  柱状图
            getCarAnaRank(carId, dateType);
        }
    });

    //故障发生数  线路平均数  排名
    function getCarAnaIdx(carId, dateType) {
        $.ajaxExt({
            url: "/carAna/getCarAnaIdx.htm",
            data: {'carId': carId, 'dateType': dateType}
        }).done(function (data) {
            $("#falutValPer").removeClass();
            $("#lineValPer").removeClass();
            $("#rankValPer").removeClass();
            if (!data) {
                $("#falutVal").html("-");
                $("#falutValPer").html("");
                $("#lineVal").html("-");
                $("#lineValPer").html("");
                $("#rankVal").html("-");
                $("#rankValPer").html("");
            } else {
                $(data).each(function (i, item) {
                    if (item.idxType == "040101") {
                        $("#falutVal").html(item.idxValue + "次");
                        var per = item.idxMomPer;
                        $("#falutValPer").html(per);
                        if (per.indexOf("-") != 0) {
                            $("#falutValPer").addClass("icon-arrow-up");
                        } else {
                            $("#falutValPer").addClass("icon-arrow-up");
                            $("#falutValPer").addClass("icon-arrow-down");
                        }
                    } else if (item.idxType == "040102") {
                        $("#lineVal").html(item.idxValue + "次");
                        var per = item.idxMomPer;
                        $("#lineValPer").html(per);
                        if (per.indexOf("-") != 0) {
                            $("#lineValPer").addClass("icon-arrow-up");
                        } else {
                            $("#lineValPer").addClass("icon-arrow-up");
                            $("#lineValPer").addClass("icon-arrow-down");
                        }
                    } else if (item.idxType == "040103") {
                        $("#rankVal").html(item.idxValue);
                        var per = item.idxMomPer;
                        $("#rankValPer").html(per);
                        if (per > 0) {
                            $("#rankValPer").addClass("icon-arrow-up");
                        } else if (per < 0) {
                            $("#rankValPer").addClass("icon-arrow-up");
                            $("#rankValPer").addClass("icon-arrow-down");
                        } else {
                            $("#rankValPer").addClass("icon-arrow-up");
                            $("#rankValPer").addClass("icon-arrow-unchanged");
                        }
                    }
                });
            }
        });
    }

    //每日故障数据
    function getCarAnaTrend(carId, dateType) {
        $.ajaxExt({
            url: "/carAna/getCarAnaTrend.htm",
            data: {'carId': carId, 'dateType': dateType}
        }).done(function (data) {
            if (data.length > 0) {
                var series = [], category = [],
                    tmpDate = data[0].REPORT_DATE;
                for (var i = 0; i < data.length; i++) {
                    if (data[i].REPORT_DATE == tmpDate) {
                        series.push({"name": data[i].TREND_VAL1, data: []});
                    } else {
                        break;
                    }
                    ;
                }
                for (var i = 0; i < data.length; i += series.length) {
                    xAxisDateFormat(dateType, category, data[i].REPORT_DATE);
                    for (var s = 0; s < series.length; s++) {
                        series[s].data.push(parseInt(data[i + s].TREND_VAL2));
                    }
                    ;
                }
                anaFaultData(titleStr, category, series);
            } else {
                anaFaultData("暂无数据", "", "");
            }
        });
    }

    //每日故障数据 图表
    function anaFaultData(titleStr, category, series) {
        var tickInter;
        if (dateType == "04" || dateType == "05") {
            tickInter = 2;
        } else {
            tickInter = 1;
        }
        $('#anaFaultData').highcharts({
            chart: {
                type: 'spline'
            },
            title: {
                text: "车辆：" + carName + ' ' + titleStr,
                style: {
                    fontFamily: 'sans-serif',
                    fontSize: '14px'
                }
            },
            subtitle: {
                text: ''
            },
            xAxis: {
                tickInterval: tickInter,
                categories: category
            },
            yAxis: {
                title: {
                    text: '次'
                }
            },
            credits: {
                enabled: false
            },
            tooltip: {
                crosshairs: true,
                shared: true
            },
            plotOptions: {
                line: {
                    dataLabels: {
                        enabled: true
                    },
                    enableMouseTracking: false
                }
            },
            series: series
        });
    }

    //故障排名  饼图  柱状图
    function getCarAnaRank() {
        $.ajaxExt({
            url: "/carAna/getCarAnaRank.htm",
            data: {'carId': carId, 'dateType': dateType}
        }).done(function (data) {
            var faultNameRadarArr = [], faultCountRadarArr = [], lineAvgRadarArr = [], faultPct = [], trHtml;
            trHtml = "<tr><th width='15%'>排名</th><th width='25%'>故障名称</th><th width='15%'>次数</th><th width='20%'>占比</th><th>线路平均值</th></tr>";
            $(data).each(function (i, item) {
                if (item.IDX_TYPE == "040301") {
                    trHtml += "<tr><td><span class='rank-tip'>" + item.RANK_SN + "</span></td><td>" + item.RANK_VAL1 + "</td><td>" + item.RANK_VAL2 + "</td><td>" + item.RANK_VAL3 + "</td><td>" + item.RANK_VAL4 + "</td></tr>";
                    //饼图  填充数据
                    faultPct.push([item.RANK_VAL1, parseInt(item.RANK_VAL2) || 0]);
                    //柱状图 填充数据   次数
                    faultNameRadarArr.push(item.RANK_VAL1);
                    faultCountRadarArr.push(parseInt(item.RANK_VAL2) || 0);
                } else if (item.IDX_TYPE == "040402") {
                    //柱状图 填充数据  线路
                    lineAvgRadarArr.push(parseInt(item.RANK_VAL4) || 0);
                }
            });
            $("#trHtml").html(trHtml);
            //柱状图
            $('#anaFaultRadar').highcharts({
                chart: {
                    type: 'column'
                },
                title: {
                    text: "车辆：" + carName + ' ' + titleStr,
                    style: {
                        fontFamily: 'sans-serif',
                        fontSize: '14px'
                    }
                },
                pane: {
                    size: '80%'
                },
                xAxis: {
                    categories: faultNameRadarArr,
                    tickmarkPlacement: 'on',
                    lineWidth: 0
                },
                yAxis: {
                    title: {
                        text: '次'
                    }
                },
                tooltip: {
                    shared: true,
                    pointFormat: '<span style="color:{series.color}">{series.name}: <b>{point.y:,.0f}</b><br/>'
                },
                credits: {
                    enabled: false
                },
                legend: {
                    align: 'center',
                    verticalAlign: 'bottom',
                    layout: 'vertical'
                },
                series: [{
                    name: '车辆次数',
                    data: faultCountRadarArr
                }, {
                    name: '线路平均次数',
                    data: lineAvgRadarArr
                }]
            });
            //饼图
            $("#anaFaultPct").highcharts({
                chart: {
                    plotBackgroundColor: null,
                    plotBorderWidth: null,
                    plotShadow: false
                },
                title: {
                    text: "车辆：" + carName + ' ' + titleStr,
                    style: {
                        fontFamily: 'sans-serif',
                        fontSize: '14px'
                    }
                },
                tooltip: {
                    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
                },
                plotOptions: {
                    pie: {
                        allowPointSelect: true,
                        cursor: 'pointer',
                        dataLabels: {
                            enabled: true,
                            color: '#000000',
                            connectorColor: '#000000',
                            format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                        }
                    }
                },
                credits: {
                    enabled: false
                },
                series: [
                    {
                        type: 'pie',
                        name: '',
                        data: faultPct
                    }
                ]
            });
        });
    }

    //如果左侧树有选择车辆则直接查询该车辆数据
    business.execFnByLeftTreeSelectNode('CAR', carChange);

    gotoCarAnaFaultInfo=function(data){
        commonFun.showLeftTree(commonFun.carTree);
        commonFun.setSelectedNodes(data.carId);
        carId=data.carId;
        carName = data.licPltNo;
        $("a[val='" + data.dateType + "']",".time-select").click();
    }
});